package com.tuniu.agents.impl.trip;


import com.tuniu.agents.agent.AgentManager;
import com.tuniu.agents.agent.AgentManagerGroup;
import com.tuniu.agents.message.MessageLogger;
import com.tuniu.agents.message.MessageSender;
import com.tuniu.agents.tools.trip.tool.ItineraryPlanListTool;
import lombok.SneakyThrows;
import java.util.HashMap;
import java.util.Map;

import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Trip Agent Bean configurations
 */
@Configuration
public class TripAgentConfig {
    // TripSearchAgent
    public static final String SEARCH_AGENT_ID = "TRIP_SEARCH_AGENT";
    public static final String SEARCH_AGENT_PROMPT_ID = "trip_search_agent";

    public static final String EXTRACTOR_AGENT_ID = "EXTRACTOR_AGENT";
    private static final String TRIP_EXTRACTOR_PROMPT = "trip-extractor";

    public static final String TRIP_SUMMARY_AGENT_ID = "TRIP_SUMMARY_AGENT";
    private static final String TRIP_SUMMARY_PROMPT = "trip-summary";
    @Value("${aigc.agents.tools.trip.search.vectorMatchTargetUrl}")
    private String vectorMatchTargetUrl;
    @Value("${aigc.agents.tools.trip.search.authToken:}")
    private String searchAuthToken;

    // Recommend
    public static final String TRIP_RECOMMEND_AGENT_ID = "RECOMMEND_AGENT";
    public static final String TRIP_RECOMMEND_AGENT_PROMPT_ID = "trip_recommend_agent";
    @Value("${aigc.agents.tools.trip.recommend.recommendTargetUrl}")
    private String tripRecommendTargetUrl;
    @Value("${aigc.agents.tools.trip.recommend.authToken:}")
    private String recommendAuthToken;

    // Compare
    public static final String TRIP_COMPARE_AGENT_ID = "COMPARE_AGENT";
    public static final String TRIP_COMPARE_AGENT_PROMPT_ID = "trip_compare_agent";
    @Value("${aigc.agents.tools.trip.compare.compareTargetUrl}")
    private String tripCompareTargetUrl;
    @Value("${aigc.agents.tools.trip.compare.authToken:}")
    private String compareAuthToken;


    @Autowired
    private AgentManager agentManager;


    @Bean(SEARCH_AGENT_ID)
    @SneakyThrows
    public OnlineSearcher searchAgent(AgentManagerGroup agentManagerGroup) {
        Map<String, String> headers = new HashMap<>();
        if (searchAuthToken != null && !searchAuthToken.isEmpty()) {
            headers.put("Authorization", "Bearer " + searchAuthToken);
        }
        headers.put("Content-Type", "application/json");
        //headers.put("Accept", "text/event-stream");
        
        return new OnlineSearcher(
                agentManagerGroup,
                SEARCH_AGENT_ID,
                SEARCH_AGENT_PROMPT_ID,
                vectorMatchTargetUrl,
                headers,
                new SearchStreamResponseHandler(agentManagerGroup.getChatEnvironmentMemory(), agentManager)
        );
    }


    @Bean(TRIP_RECOMMEND_AGENT_ID)
    @SneakyThrows
    public Recommend tripRecommendAgent(AgentManagerGroup agentManagerGroup) {
        Map<String, String> headers = new HashMap<>();
        if (recommendAuthToken != null && !recommendAuthToken.isEmpty()) {
            headers.put("Authorization", "Bearer " + recommendAuthToken);
        }
        headers.put("Content-Type", "application/json");
        headers.put("Accept", "application/json");
        
        return new Recommend(
                agentManagerGroup,
                TRIP_RECOMMEND_AGENT_ID,
                TRIP_RECOMMEND_AGENT_PROMPT_ID,
                tripRecommendTargetUrl,
                headers,
                agentManagerGroup.getChatEnvironmentMemory()
        );
    }


    @Bean(TRIP_COMPARE_AGENT_ID)
    @SneakyThrows
    public Compare tripCompareAgent(AgentManagerGroup agentManagerGroup) {
        Map<String, String> headers = new HashMap<>();
        if (compareAuthToken != null && !compareAuthToken.isEmpty()) {
            headers.put("Authorization", "Bearer " + compareAuthToken);
        }
        headers.put("Content-Type", "application/json");
        headers.put("Accept", "application/json");
        
        return new Compare(
                agentManagerGroup,
                TRIP_COMPARE_AGENT_ID,
                TRIP_COMPARE_AGENT_PROMPT_ID,
                tripCompareTargetUrl,
                headers,
                agentManagerGroup.getChatEnvironmentMemory()
        );
    }



    @Bean(EXTRACTOR_AGENT_ID)
    @SneakyThrows
    public Extractor extractor(AgentManagerGroup agentManagerGroup, MessageSender messageSender, @Qualifier("qwen-vl-max-latest") ChatOptions chatOptions) {
        MessageLogger messageLogger = MessageLogger.builder().beforeMessage("Calling Trip Analysis Assistant").build();
        return new Extractor(agentManagerGroup, TripAgentConfig.EXTRACTOR_AGENT_ID, messageSender, TRIP_EXTRACTOR_PROMPT, messageLogger, chatOptions);
    }

    @Bean(TRIP_SUMMARY_AGENT_ID)
    @SneakyThrows
    public TripSummaryAgent tripSummaryAgent(AgentManagerGroup agentManagerGroup, MessageSender messageSender) {
        MessageLogger messageLogger = MessageLogger.builder().beforeMessage("Calling Trip Breakdown Assistant").build();
        return new TripSummaryAgent(agentManagerGroup, TripAgentConfig.TRIP_SUMMARY_AGENT_ID,  TRIP_SUMMARY_PROMPT);
    }

    @Bean(Designer.AGENT_ID)
    @SneakyThrows
    public Designer designer(AgentManagerGroup agentManagerGroup) {
        return new Designer(agentManagerGroup, Designer.AGENT_ID, Designer.PROMPT_ID, ItineraryPlanListTool.TOOLNAME);
    }
} 
